package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class SplitsTheGivenArrayIntoBlocksOfGivenSize {
	public static void main(String[] argv) {
		byte[] byteArray = new byte[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		int blocksize = 2;
		System.out.println(splitAndPad(byteArray, blocksize));
	}

	public static List<byte[]> splitAndPad(byte[] byteArray, int blocksize) {
		List<byte[]> blocks = new ArrayList<byte[]>();
		int numBlocks = (int) Math.ceil(byteArray.length
				/ (double) blocksize);

		for (int i = 0; i < numBlocks; i++) {

			byte[] block = new byte[blocksize];
			Arrays.fill(block, (byte) 0x00);
			if (i + 1 == numBlocks) {
				int remainingBytes = byteArray.length - (i * blocksize);
				System.arraycopy(byteArray, i * blocksize, block, 0,
						remainingBytes);
			} else {
				System.arraycopy(byteArray, i * blocksize, block, 0,
						blocksize);
			}
			blocks.add(block);
		}

		return blocks;
	}
}
